package core.objects;

import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class Book extends BaseObject {

    private ArrayList<Category> categories = null;
    private ArrayList<Author> authors = null;
    private ArrayList<Publisher> publishers = null;

    private String hasISBN = null;
    private int numOfPages = 0;
    private Date releaseYear = null;
    private String wikipediaURL = null;
    private String description = null;
    private double googleRating = 0;
    private URL thumbnailURL = null;
    private String language = null;

    public Book(int id, String name) {
        super(id, name);
    }
    
    public Book(int id, String name, String isbn, Date publicationYear,int pageCount, String wikiUrl,
            URL thumbnailURL, String description, double googleRating, String language) {
        super(id, name);
        this.hasISBN = isbn;
        this.releaseYear = publicationYear;
        this.wikipediaURL = wikiUrl;
        this.numOfPages = pageCount;
        this.thumbnailURL = thumbnailURL;
        this.description = description;
        this.googleRating = googleRating;
        this.language = language;
    }

    public void addCategory(Category category) {

        //initialize array list when first needed
        //(no reason to take up so much space right off the bat):
        if (categories == null) {
            categories = new ArrayList<Category>();
        }

        /* make sure that if we're merging two books together
         * (partial data from two instances), we don't hold
         * duplicate data: */
        if (!categories.contains(category)) {
            categories.add(category);
        }
    }

    public ArrayList<Category> getCategories() {
        return categories;
    }
    
    public void setCategories(ArrayList<Category> categories) {
        this.categories = categories;
    }

    public ArrayList<Author> getAuthors() {
        return authors;
    }
    
    public void setAuthors(ArrayList<Author> authors) {
        this.authors = authors;
    }

    public void addAuthor(Author author) {

        if (authors == null) {
            authors = new ArrayList<Author>();
        }

        /* make sure that if we're merging two books together
         * (partial data from two instances), we don't hold
         * duplicate data: */
        authors.add(author);
    }

    public void setISBN(String isbn) {
        hasISBN = isbn;
    }

    public String getISBN() {
        return hasISBN;
    }

    public void setNumOfPages(int num) {
        if (num > 0) {
            numOfPages = num;
        }
    }

    public int getNumOfPages() {
        return numOfPages;
    }

    public void setReleaseYear(Date year) {
        releaseYear = year;
    }

    public Date getReleaseYear() {
        return releaseYear;
    }

    public void setWikipediaURL(String url) {
        wikipediaURL = url;
    }

    public String getWikipediaURL() {
        return wikipediaURL;
    }
    
    public void setDescription(String desc) {
    	description = desc;
    }
    
    public String getDescription() {
    	return description;
    }
    
    public void setGoogleRating(double rating) {
    	googleRating = rating;
    }
    
    public double getGoogleRating() {
    	return googleRating;
    }
    
    public void setThumbnailURL(URL url) {
    	thumbnailURL = url;
    }
    
    public URL getThumbnailURL() {
    	return thumbnailURL;
    }
    
    public void setLanguage(String lang) {
    	language = lang;
    }
    
    public String getLanguage() {
    	return language;
    }

    public void addPublisher(Publisher publisher) {

        //initialize array list when first needed
        //(no reason to take up so much space right off the bat):
        if (publishers == null) {
            publishers = new ArrayList<Publisher>();
        }

        /* make sure that if we're merging two books together
         * (partial data from two instances), we don't hold
         * duplicate data: */
        if (!publishers.contains(publisher)) {
            publishers.add(publisher);
        }
    }

    public ArrayList<Publisher> getPublishers() {
        return publishers;
    }
    
    public void setPublishers(ArrayList<Publisher> publishers) {
        this.publishers = publishers;
    }

    @Override
    public Object[] toObjectArray() {
    	Calendar c = Calendar.getInstance();
    	if (getReleaseYear() != null) {
    		c.setTime(getReleaseYear());
    	}
        return new Object[]{ getName(), (getReleaseYear()==null ? 0 : c.get(Calendar.YEAR)),
            getNumOfPages(), getISBN(), getWikipediaURL(), getThumbnailURL(), getDescription(), getGoogleRating(), 
            getLanguage()
        };
    }
}
